package personal.hmm.hpbd;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class StartScreen extends Activity {
	private final String PASSWORD = "HMM132";
	private final String HMM_BIRTHDAY = "13/02";
	private EditText edPassword;
	private Button btContinue;
	private byte checkType = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start);

		// All widgets
		edPassword = (EditText) findViewById(R.id.edPassword);
		btContinue = (Button) findViewById(R.id.btContinue);

		btContinue.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Calendar calendar = Calendar.getInstance();
				SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM");
				String formatDate = dateFormat.format(calendar.getTime());
				if (formatDate.equals(HMM_BIRTHDAY)) {
					if (checkPassword()) {
						startActivity(new Intent("personal.hmm.hpbd.AUTHEN"));
					} else {
						Toast.makeText(
								getApplicationContext(),
								"Your password is not correct!\nYou have "
										+ (5 - checkType) + " times to try.",
								Toast.LENGTH_SHORT).show();
						if (checkType == 5) {
							System.exit(0);
						}
					}
				} else {
					Toast.makeText(
							getApplicationContext(),
							"Hôm nay là "
									+ dateFormat.format(calendar.getTime())
									+ " mừ. " + ":-P Chờ đến 13/02 em nhé! :D",
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	private boolean checkPassword() {
		if (this.edPassword.getText().toString().equals(PASSWORD)) {
			checkType = 0;
			edPassword.setText(null);
			return true;
		} else {
			++checkType;
			edPassword.setText(null);
			return false;
		}
	}
}
